package com.weiaibenpao.demo.chislim.hurui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.hurui.activity.HWebViewActivity;
import com.weiaibenpao.demo.chislim.hurui.activity.RunningMachineActivity;
import com.weiaibenpao.demo.chislim.hurui.activity.TourismActivity;
import com.weiaibenpao.demo.chislim.hurui.adapte.HrFristAdapter;
import com.weiaibenpao.demo.chislim.hurui.base.HBaseFragment;
import com.weiaibenpao.demo.chislim.hurui.bean.BannerItemBean;
import com.weiaibenpao.demo.chislim.hurui.bean.FlexibleBean;
import com.weiaibenpao.demo.chislim.hurui.utils.Utils;
import com.weiaibenpao.demo.chislim.ui.MusicTypeActivity;
import com.weiaibenpao.demo.chislim.util.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by lenovo on 2017/4/10.
 */

public class HR_Frist_Fragment extends HBaseFragment {

    @BindView(R.id.mylists)
    ListView listView ;

    private View rootView ;

    private View headView ; //头布局

    private Banner banner ; //轮播图

    private LinearLayout gotoRunmacheine ; //去跑步机的界面

    private LinearLayout gotoMusic ; //去音乐的界面

    private LinearLayout gotoTourism ; // 旅游的界面

    private List<FlexibleBean.DataBean.ActivitylistBean> lists ; // 活动数据

    private List<BannerItemBean.DataBean.ImgCastinglistBean> bannlists ; //banner数据

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null){
            rootView  = inflater.inflate(R.layout.hr_frist_fragment_2 , null);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initEvent();
    }

    private void initView() {
        if(headView == null){
            headView = LayoutInflater.from(getActivity()).inflate(R.layout.hr_frist_fragment_headview , null);
            initHeadView(headView);
        }

        //判断是否已经添加了头布局，如果已经添加了则不在添加
        if(listView.getHeaderViewsCount() == 0){
            listView.addHeaderView(headView);
            initData();
        }
    }

    /**
     * 初始化头布局里面的view
     */
    private void initHeadView(View head) {
        banner = (Banner) head.findViewById(R.id.banner);
        gotoRunmacheine = (LinearLayout) head.findViewById(R.id.gotoRunmacheine);
        gotoMusic = (LinearLayout) head.findViewById(R.id.gotoMusic);
        gotoTourism = (LinearLayout)head.findViewById(R.id.gotoTourism);


        banner.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.ScaleInOut);
        //设置标题集合（当banner样式有显示title时）
        //banner.setBannerTitles(Arrays.asList(titles));
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(4000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
    }

    /**
     * 网络请求数据
     */
    private void initData() {
        initBannerData();
        initActivityData();
    }



    /**
     * 轮播图的网络请求
     */
    private void initBannerData() {
        Call<BannerItemBean> call = apiStores.getBannerImageService("","1");
        call.enqueue(new Callback<BannerItemBean>() {
            @Override
            public void onResponse(Call<BannerItemBean> call, Response<BannerItemBean> response) {
                if(response.isSuccessful()){
                    BannerItemBean bannerbean = response.body() ;
                    bannlists = bannerbean.getData().getImgCastinglist() ;
                    List<String> imageUrl = new ArrayList<String>();
                    for(BannerItemBean.DataBean.ImgCastinglistBean b :
                            bannerbean.getData().getImgCastinglist()){
                        imageUrl.add(b.getImgCastingImgUrl());
                    }
                    banner.setImages(imageUrl);
                    banner.start();
                }
            }

            @Override
            public void onFailure(Call<BannerItemBean> call, Throwable t) {

            }
        });

        //tianjia call
        addCalls(call);
    }

    /**
     * 活动数据
     */
    public void initActivityData(){
        Call<FlexibleBean> call = apiStores.getFlexibleBeanService("0" , "");
        call.enqueue(new Callback<FlexibleBean>() {
            @Override
            public void onResponse(Call<FlexibleBean> call, Response<FlexibleBean> response) {
                if(response.isSuccessful()){
                    FlexibleBean bean = response.body() ;
                    if(bean.getCode() == 0){
                        lists = bean.getData().getActivitylist();
                        listView.setAdapter(new HrFristAdapter(getActivity() , lists));
                    }
                }
            }

            @Override
            public void onFailure(Call<FlexibleBean> call, Throwable t) {

            }
        });
    }

    //事件
    private void initEvent() {
        gotoRunmacheine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , RunningMachineActivity.class) ;
                startActivity(intent);
            }
        });

        gotoTourism.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , TourismActivity.class);
                startActivity(intent);
            }
        });

        gotoMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , MusicTypeActivity.class);
                startActivity(intent);
            }
        });

        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Utils.gotoTmailShop(getActivity() ,
                        bannlists.get(position - 1).getImgCastingTitle() , bannlists.get(position - 1).getImgCastingContentUrl());
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(mActivity, position+"", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity() , HWebViewActivity.class);
                intent.putExtra("urlpath" , lists.get(position - 1).getImgCastingContentUrl());
                intent.putExtra("title" , (lists.get(position - 1).getActivityTitle().split("#"))[0] );
                startActivity(intent);
            }
        });
    }
}
